/*
 * Powered By dico
 * Since 2017 - 2018
 */
package com.seeyoui.kensite.bussiness.orderlang.productLabel.controller;

import java.sql.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.seeyoui.kensite.common.util.DateUtils;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.base.domain.EasyUIDataGrid;
import com.seeyoui.kensite.common.base.controller.BaseController;
import com.seeyoui.kensite.common.util.RequestResponseUtil;
import com.seeyoui.kensite.common.util.Result;
import com.seeyoui.kensite.common.util.excel.ExportExcel;

import com.seeyoui.kensite.common.base.domain.Page;
import com.alibaba.fastjson.JSONPath;
import com.google.gson.Gson;
import com.seeyoui.kensite.bussiness.orderlang.productLabel.domain.ProductLabel;
import com.seeyoui.kensite.bussiness.orderlang.productLabel.service.ProductLabelService;
/**
 * ProductLabel
 * @author dico
 * @version 2.0
 * @since 1.0
 * @date 2018-02-07
 */

@Controller
@RequestMapping(value = "productLabel/productLabel")
public class ProductLabelController extends BaseController {
	
	@Autowired
	private ProductLabelService productLabelService;
	
	/**
	 * 商品新增页面获取标签列表
	 * @return
	 */
	@RequestMapping(value = "/productLabelList", method=RequestMethod.POST)
	@ResponseBody
	public Result findProductLabelListNoRepeat() throws Exception {
		System.out.println("ProductLabelController.findProductLabelListNoRepeat()");
		List<ProductLabel> productLabelList = productLabelService.findProductLabelListNoRepeat();
		if(productLabelList.isEmpty()){
			return Result.failure("暂无数据");
		}else{
			return Result.success(productLabelList);
		}
	}
	
	/**
	 * 根据商品id查询标签列表
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/labelListByProductId", method=RequestMethod.POST)
	@ResponseBody
	public Result findlabelByProductId(String productId) throws Exception {
		List<ProductLabel> labelList = productLabelService.findlabelByProductId(productId);
		if(labelList.size() > 0){
			return Result.success(labelList);
		}else{
			return Result.failure("查询失败");
		}
	}
	
	/**
	 * 展示列表页面
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:view")
	@RequestMapping(value = "/{page}")
	public ModelAndView view(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page) throws Exception {
		return new ModelAndView("bussiness/orderlang/productLabel/"+page, modelMap);
	}
	
	/**
	 * 根据ID查询单条数据
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:select")
	@RequestMapping(value = "/data/{id}")
	@ResponseBody
	public Object data(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String id) throws Exception {
		ProductLabel productLabel = productLabelService.findOne(id);
		return productLabel;
	}
	
	/**
	 * 根据ID查询单条数据并返回相应表单
	 * @param modelMap
	 * @param module
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:view")
	@RequestMapping(value = "/form/{page}/{id}")
	public ModelAndView form(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, @PathVariable String page, @PathVariable String id) throws Exception {
		ProductLabel productLabel = productLabelService.findOne(id);
		modelMap.put("productLabel", productLabel);
		return new ModelAndView("bussiness/orderlang/productLabel/"+page, modelMap);
	}
	
	/**
	 * 获取列表展示数据
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:select")
	@RequestMapping(value = "/list/data", method=RequestMethod.POST)
	@ResponseBody
	public Object listData(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		List<ProductLabel> productLabelList = productLabelService.findList(productLabel);
		int total = productLabelService.findTotal(productLabel);
		EasyUIDataGrid eudg = new EasyUIDataGrid();
		eudg.setTotal(String.valueOf(total));
		eudg.setRows(productLabelList);
		return eudg;
	}
	
	/**
	 * 获取列表展示数据带分页器用于jsp自己做分页
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:view")
	@RequestMapping(value = "/list/page", method=RequestMethod.POST)
	@ResponseBody
	public Object listPage(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		List<ProductLabel> productLabelList = productLabelService.findList(productLabel);
		int total = productLabelService.findTotal(productLabel);
		Page<ProductLabel> page = new Page<ProductLabel>(productLabel.getPage(), productLabel.getRows(), total);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("rows", productLabelList);
		dataMap.put("page", page);
		return dataMap;
	}
	
	/**
	 * 获取所有数据
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:select")
	@RequestMapping(value = "/list/all", method=RequestMethod.POST)
	@ResponseBody
	public Object listAll(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		List<ProductLabel> productLabelList = productLabelService.findAll(productLabel);
		return productLabelList;
	}
	
	/**
	 * 保存新增的数据
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:insert")
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		if (!beanValidator(modelMap, productLabel)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		productLabelService.save(productLabel);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 保存修改的数据
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:update")
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		if (!beanValidator(modelMap, productLabel)){
			RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.FALSE);
			return null;
		}
		productLabelService.update(productLabel);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 删除数据库
	 * @param modelMap
	 * @param productLabelId
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:delete")
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, String id) throws Exception {
		List<String> listId = Arrays.asList(id.split(","));
		productLabelService.delete(listId);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}
	
	/**
	 * 假删除
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:delete")
	@RequestMapping(value = "/remove", method=RequestMethod.POST)
	@ResponseBody
	public String remove(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		productLabelService.remove(productLabel);
		RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
		return null;
	}

	/**
	 * 导出Excel数据
	 * @param modelMap
	 * @param productLabel
	 * @return
	 * @throws Exception
	 */
	//@RequiresPermissions("productLabel:productLabel:export")
	@RequestMapping(value = "/export")
	public String export(HttpSession session,
			HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap, ProductLabel productLabel) throws Exception {
		String fileName = DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
		List<ProductLabel> productLabelList = productLabelService.findAll(productLabel);
		new ExportExcel(null, ProductLabel.class).setDataList(productLabelList).write(response, fileName).dispose();
		return null;
	}
	
	/**
	 * 添加商品标签
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	
	//  请求数据格式   {"prodLableJson":{"productId":"","label":"新年货"}
	@RequestMapping(value = "/addProdLable", method=RequestMethod.POST)
	@ResponseBody
	public Result addProdLable(HttpServletResponse response, HttpServletRequest request) {
		String json = request.getParameter("prodLableJson");
		
		if (json!=null&&!json.equals("")) {
			ProductLabel pLabel=new Gson().fromJson(json, ProductLabel.class);
			if(pLabel!=null) {
				productLabelService.save(pLabel);
				return Result.success(null, "商品标签添加成功");
			}else {
				return Result.failure("商品标签添加失败");
			}
			
		}
		return null;
	}
	
	
	/**
	 * 删除商品标签数据
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	
	//  请求数据格式    {"id":""}
	@RequestMapping(value = "/deleteProdLable", method=RequestMethod.POST)
	@ResponseBody
	public Result deleteProdLable(HttpServletResponse response, HttpServletRequest request) {
		String id=request.getParameter("id");
		
		if (id!=null&&!id.equals("")) {
			productLabelService.delete(Arrays.asList(id.split(",")));
			return Result.success(null, "标签删除成功");
		}else {
			return Result.failure("标签删除失败");
		}
	}
	
	/**
	 * 修改商品标签
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	
	//  请求数据格式  { "prodLableJson" :{"id":"","productId":"","lable":""}}
	@RequestMapping(value = "/updeteProdLable", method=RequestMethod.POST)
	@ResponseBody
	public Result updeteProdLable(HttpServletResponse response, HttpServletRequest request) {
		String json = request.getParameter("prodLableJson");
		
		if (json!=null&&!json.equals("")) {
			 String id= (String) JSONPath.read(json, "$.id");
			 ProductLabel pLabel=productLabelService.findOne(id);
			 if (pLabel!=null) {
				pLabel.setProductId((String)JSONPath.read(json, "$.productId"));
				pLabel.setLabel((String)JSONPath.read(json, "$.lable"));
				productLabelService.update(pLabel);
				return Result.success(null, "标签修改成功");
			}else {
				return Result.failure("标签修改失败");
			}
			
		}
		
		return null;
	}
	
	/**
	 * 查询所有商品标签
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findAllProdLable", method=RequestMethod.POST)
	@ResponseBody
	public Result findAllProdLable (HttpServletResponse response, HttpServletRequest request,ProductLabel pLabel) {
		pLabel.setDelFlag("0");
		List<ProductLabel> list =new ArrayList<>();
		if (list!=null&&list.size()>0) {
			productLabelService.findAll(pLabel);
			return Result.success(list, "查询成功");
			
		}else {
			return Result.failure("查询失败");
		}
	}
	
}